/**
 * 
 */
package com.jolestar.yayaweibotong.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jolestar.yayaweibotong.dao.InviteTokenDomain;
import com.jolestar.yayaweibotong.service.UserService;

/**
 * @author jolestar@gmail.com
 * 
 */
public class InviteTokenAction extends AbstractAction {

	public InviteTokenAction() {
		super("invite-token");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jolestar.yayaweibotong.controller.AbstractAction#doAction(javax.servlet
	 * .http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected String doAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		UserService userService = UserService.ServiceHolder.getInstance();
		List<InviteTokenDomain> tokens;
		if ("generate".equals(type)) {
			userService.generateInviteToken(10);
			tokens = userService.getUnUsedTokens();
		} else if ("used".equals(type)) {
			request.setAttribute("used", true);
			tokens = userService.getUsedTokens();
		} else {
			request.setAttribute("used", false);
			tokens = userService.getUnUsedTokens();
		}
		request.setAttribute("tokens", tokens);
		return "invite_token";
	}

	@Override
	public String getPath() {
		return "/admin/" + this.getName();
	}

}
